//
//  CMDBRegisterTopView.swift
//  CMDB
//
//  Created by 王磊 on 05/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

public let CMDB_Register_TF_Height: CGFloat = 60

public let CMDB_Register_TF_FONT: UIFont = CMDB_16_FONT

public let CMDB_Register_TF_FONT_Size: CGFloat = 16

public let CMDB_Register_TF_PlaceHolder_Color: UIColor = UIColor.lightGrayColor()

class CMDBRegisterTopView: TSBaseView {
    
    internal var phone: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var verify: CMDBRegisterVcodeTF = CMDBRegisterVcodeTF()
    
    internal var invite: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var name: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
}
extension CMDBRegisterTopView {
    
    
}

extension CMDBRegisterTopView {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(phone)
        
        addSubview(verify)
        
        addSubview(invite)
        
        addSubview(name)
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        let starAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: UIColor.Hex6(0xff6d00, alpha: 54),NSKernAttributeName: 1]
        
        let starText = "*"
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        var mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "手机号码:", attributes: textAttr))
        
        phone.attrText = mutable
        
        phone.placeHolderAttr = NSAttributedString(string: "请输入您的手机号", attributes: placeHolderAttr)
        
        phone.keyboardType = .NumberPad
        
        phone.returnKeyType = .Done
        
        phone.maxLength = 11
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "验证码:", attributes: textAttr))
        
        verify.attrText = mutable
        
        verify.placeHolderAttr = NSAttributedString(string: "请输入验证码", attributes: placeHolderAttr)
        
        verify.keyboardType = .NumberPad
        
        verify.returnKeyType = .Done
        
        verify.maxLength = VCode_Length
        
        verify.addTarget(self, action: #selector(keyboardReturn), controlEvent: .Vcode)
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "邀请码:", attributes: textAttr))
        
        invite.attrText = mutable
        
        invite.placeHolderAttr = NSAttributedString(string: "请输入邀请码", attributes: placeHolderAttr)
        
        invite.keyboardType = .Default
        
        invite.returnKeyType = .Next
        
        invite.maxLength = InviteCode_Length
        
        invite.addTarget(self, action: #selector(keyboardReturn), controlEvent: .ReturnKey)
        
        //
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "姓名:", attributes: textAttr))
        
        name.placeHolderAttr = NSAttributedString(string: "请输入姓名", attributes: placeHolderAttr)
        
        name.attrText = mutable
        
        name.keyboardType = .Default
        
        name.returnKeyType = .Next
        
        name.maxLength = 25
        
        name.addTarget(self, action: #selector(keyboardReturn), controlEvent: .ReturnKey)
        
    }
}
extension CMDBRegisterTopView {
    
    @objc private func keyboardReturn(argu: ArguInfo) {
        
        if argu.view == invite {
            
            invite.resignFirstResponder()
            
            name.becomeFirstResponder()
        } else {
            
            name.resignFirstResponder()
            
            if let target = unsureTarget {
                
                TSRuntimeManager.getInstance().addTarget(target, andSel: unsureAction!, andAnyArgu: argu)
            }
        }
    }
}

extension CMDBRegisterTopView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        phone.snp_makeConstraints { (make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(0)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        verify.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.phone.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
        invite.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.verify.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
        name.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.invite.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
    }
}